/**
 * 
 */
package org.yelongframework.spring.web.servlet.config;

import java.util.Collections;
import java.util.List;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.yelongframework.lang.Nullable;

/**
 * 处理器拦截器配置器
 * 
 * @since 1.0.0
 */
public class HandlerInterceptorConfigurer implements WebMvcConfigurer {

	/**
	 * 需要配置的处理器拦截器
	 */
	@Nullable
	protected final List<HandlerInterceptor> handlerInterceptors;

	/**
	 * @param handlerInterceptors 需要配置的处理器拦截器
	 */
	public HandlerInterceptorConfigurer(@Nullable List<HandlerInterceptor> handlerInterceptors) {
		if (null == handlerInterceptors) {
			handlerInterceptors = Collections.emptyList();
		}
		this.handlerInterceptors = handlerInterceptors;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		for (HandlerInterceptor handlerInterceptor : getHandlerInterceptors()) {
			addHandlerInterceptor(handlerInterceptor, registry);
		}
	}

	/**
	 * 添加处理器拦截器
	 * 
	 * @param handlerInterceptor 处理器拦截器
	 * @param registry           注册器
	 */
	protected void addHandlerInterceptor(HandlerInterceptor handlerInterceptor, InterceptorRegistry registry) {
		InterceptorRegistration interceptor = registry.addInterceptor(handlerInterceptor);
		interceptor.addPathPatterns("/**");
		// 排除不拦截的
		interceptor.excludePathPatterns();
	}

	/**
	 * @return 获取处理器拦截器
	 */
	public List<HandlerInterceptor> getHandlerInterceptors() {
		return handlerInterceptors;
	}

}
